package com.ssm.controller.portal;

import com.ssm.common.Const;
import com.ssm.common.ResponseCode;
import com.ssm.common.ServerResponse;
import com.ssm.entity.pojo.MmallUser;
import com.ssm.service.IUserService;
import com.ssm.service.impl.UserServiceImpl;
import net.sf.jsqlparser.schema.Server;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpSession;
import java.util.Date;

@Controller
@RequestMapping("/user/")
public class UserController {
    @Autowired
    private IUserService iUserService;
    /*
    * 用户登录
    * */
    @ResponseBody
    @RequestMapping(value = "login",method = RequestMethod.POST)
    public ServerResponse<MmallUser> login(String username, String password, HttpSession session){
        ServerResponse<MmallUser> response = iUserService.login(username,password);
        if(response.isSuccess()){
            session.setAttribute(Const.CURRENT_USER,response.getData());
        }
        return response;
    }

    /*
    *   用户登出
    * */
    @RequestMapping(value = "logout",method = RequestMethod.GET)
    public ServerResponse<String> logout(HttpSession session){
        session.removeAttribute(Const.CURRENT_USER);
        return ServerResponse.createBySuccess();
    }
    /*
    *   用户注册
    * */
    @ResponseBody
    @RequestMapping(value = "register",method = RequestMethod.GET)
    public ServerResponse<String> register(MmallUser user){
        return  iUserService.register(user);
    }

    @ResponseBody
    @RequestMapping(value = "check_valid",method = RequestMethod.GET)
    public ServerResponse<String> checkValid(String str,String strType){
        return iUserService.checkValid(str,strType);
    }
    @ResponseBody
    @RequestMapping(value = "get_user_info",method = RequestMethod.GET)
    public ServerResponse<MmallUser> getUserInfo(HttpSession session){
        MmallUser user = (MmallUser) session.getAttribute(Const.CURRENT_USER);
        if(user!=null){
            return ServerResponse.createBySuccess(user);
        }else {
            return ServerResponse.createByErrorMessage("用户未登录");
        }
    }

    /*
    *   忘记密码获取问题
    * */
    @ResponseBody
    @RequestMapping(value = "forget_get_question",method = RequestMethod.GET)
    public ServerResponse<String> forgetGetQuestion(String username){
        return iUserService.selectQuestionByUsername(username);
    }
    /*
    *   校验答案
    * */
    @ResponseBody
    @RequestMapping(value = "forget_check_answer")
    public ServerResponse<String> forgetCheckAnswer(String username,String question,String answer){
        return iUserService.checkAnswer(username,question,answer);
    }
    /*
    * 修改密码
    * */
    @RequestMapping(value = "forget_reset_password")
    @ResponseBody
    public ServerResponse<String> forgetResetPassword(String username,String passwordnew,String forgetToken){
        return  iUserService.forgetResetPassword(username,passwordnew,forgetToken);
    }

    @RequestMapping(value = "reset_password",method = RequestMethod.POST)
    @ResponseBody
    public ServerResponse<String> resetPassword(HttpSession session,String password,String passwordnew){
        MmallUser user = (MmallUser)session.getAttribute(Const.CURRENT_USER);
        if(user ==  null){
            return ServerResponse.createByErrorMessage("用户未登陆");
        }else{
            return iUserService.resetPassword(user,password,passwordnew);
        }
    }

    @ResponseBody
    @RequestMapping(value = "update_infomation",method = RequestMethod.POST)
    public ServerResponse<String> updateInformation(HttpSession session,MmallUser user){
        //判断用户是否登陆
        MmallUser u = (MmallUser)session.getAttribute(Const.CURRENT_USER);
        if(u == null){
            return ServerResponse.createByErrorMessage("用户未登录");
        }
        //获取用户主键 id
        user.setId(u.getId());
        user.setUsername(u.getUsername());
        ServerResponse<MmallUser> response = iUserService.updateInfomation(user);
        if(response.isSuccess()){
            return ServerResponse.createBySuccessMessage("更新成功");
        }
        return ServerResponse.createByErrorMessage("更新失败");
    }

    @ResponseBody
    @RequestMapping(value = "get_user_information")
    public ServerResponse<MmallUser> getUserInformation(HttpSession session){
        MmallUser user = (MmallUser)session.getAttribute(Const.CURRENT_USER);
        if(user == null){
            return ServerResponse.createByErrorCodeMessage(ResponseCode.NEED_LOGIN.getCode(),"需要登陆");
        }
        return iUserService.getUserInformation(user.getId());
    }
}
